package com.pb.infra.oth.util;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class LogUtil {

    private LogUtil() {
        throw new IllegalStateException("Utility class");
    }

    public static void info(String message, Object... params) {
        log.info(buildMessage(message, params));
    }

    public static void error(String message, Object... params) {
        log.info(buildMessage(message, params));
    }

    public static void error(Throwable e, String message, Object... params) {
        log.info(buildMessage(message, params), e);
    }

    public static String buildMessage(String message, Object... params) {
        StringBuilder builder = new StringBuilder();
        builder.append(message).append("|").append(buildParams(params));
        return builder.toString();
    }

    public static Map<Object, Object> buildParams(Object... params) {
        Map<Object, Object> paramMap = new HashMap<>();
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length;) {
                Object key = params[i];
                Object value = null;
                if (++i < params.length) {
                    value = params[i];
                }
                paramMap.put(key, value);
                i++;
            }
        }
        return paramMap;
    }

}
